package org.hackystat.devcathlon.ui.page.scoreboard.performerpanel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.user.User;

/**
 * The performer's panel displays the top users in a match.
 *
 * @author Daniel Arakaki
 */
public class FrontPerformerPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Set up lists for teams and users for display in panel.
   *
   * @param wicketId The id from the scoreboard page.
   * @param matchNames The list of match names.
   */
  public FrontPerformerPanel(String wicketId, final List<String> matchNames) {
    super(wicketId);
   
    String noMatch = "";
    if (matchNames.isEmpty()) {
      noMatch = "No matches...";
     
      String matchName = "";
      Integer point;
      List<User> userList = new ArrayList<User>();
      final Map<User, Integer> userScoreMap = new HashMap<User, Integer>();
     
      List<Team> teamsList = new ArrayList<Team>();
      List<Integer> pointsList = new ArrayList<Integer>();
   
      for (Team teamData : teamsList) {
        userList.addAll(teamData.getMembers());
        userList.add(teamData.getOwner());
        for (User userData : userList) {
          point = MatchManager.getInstance().getMatch(matchName).getTotalScore(userData);
          pointsList.add(point);
          userScoreMap.put(userData, point);
        }
      }

      //use a comparator for sorting
      add(new Label("matchHeader", ""));
     
      DataView dataView = new DataView("performerList", new ListDataProvider(userList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          User userItem = (User) item.getModelObject();
          String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
         
          item.add(new Label("performers", userName));
          item.add(new Label("points", userScoreMap.get(userItem).toString()));
        }
       
      };
      add(dataView);
    }
    else {
      String matchName = matchNames.get(0);
      Integer point;
      List<User> userList = new ArrayList<User>();
      final Map<User, Integer> userScoreMap = new HashMap<User, Integer>();
      final Map<Integer, String> scoreUserMap = new HashMap<Integer, String>();
     
      List<Team> teamsList =
        new ArrayList<Team>(MatchManager.getInstance().getMatch(matchName).getTeams());
     
      String name = "";
     
      for (Team teamData : teamsList) {
        userList.addAll(teamData.getMembers());
//        userList.add(teamData.getOwner());
        for (User userData : userList) {
          point = MatchManager.getInstance().getMatch(matchName).getTotalScore(userData);
          userScoreMap.put(userData, point);
          name = userData.getFirstName().concat(" " + userData.getLastName());
          scoreUserMap.put(point, name);
        }
      }
     
      // Need to sort the scores
      //final TreeMap<User, Integer> sortedMap = new TreeMap<User, Integer>(userScoreMap);
      //List<User> userKeys = new ArrayList<User>(userScoreMap.keySet());
      List<Integer> userScores = new ArrayList<Integer>(userScoreMap.values());
      TreeSet<Integer> sortedSet = new TreeSet<Integer>(userScores);

      Object[] scoreArray = sortedSet.toArray();
     
      List<Integer> scores = new ArrayList<Integer>();
     
      int size = sortedSet.size();
     
      for (int i = size - 1; i >= 0; i--) {
        scores.add((Integer) scoreArray[i]);
      }
     
      //use a comparator for sorting
      add(new Label("matchHeader", MatchManager.getInstance().getMatch(matchName).getName()));
     
      DataView dataView = new DataView("performerList", new ListDataProvider(scores)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          Integer numItem = (Integer) item.getModelObject();
          //User userItem = (User) item.getModelObject();
          //String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
         
          item.add(new Label("performers", scoreUserMap.get(numItem)));
          item.add(new Label("points", numItem.toString()));
        }
       
      };
      add(dataView);
    }
    add(new Label("noMatch", noMatch));  
  }

}

